using System;
using System.Collections;
using System.Collections.Generic;
using PEProtocol;
using UnityEngine;
using Object = UnityEngine.Object;


public class UISvc : ILogic
{
    private PERoot mRoot;
    private UI_Loading mUILoading;
    private UI_Tips mTips;
    private readonly Dictionary<UIWindow, WindowRoot> mUIWindowDic = new Dictionary<UIWindow, WindowRoot>();

    public void Init()
    {
        mRoot =PERoot.Instance;
        mUILoading = mRoot.transform.Find("Canvas/UI_Loading").GetComponent<UI_Loading>();
        mUIWindowDic.Add(UIWindow.Loading,mUILoading);
        mTips= mRoot.transform.Find("Canvas/UI_Tips").GetComponent<UI_Tips>();
        mUIWindowDic.Add(UIWindow.Tips,mTips);
        
        UI_Login login = mRoot.transform.Find("Canvas/UI_Login").GetComponent<UI_Login>();
        mUIWindowDic.Add(UIWindow.Login,login);
        UI_Create create = mRoot.transform.Find("Canvas/UI_Create").GetComponent<UI_Create>();
        mUIWindowDic.Add(UIWindow.Create,create);
        UI_Major major = mRoot.transform.Find("Canvas/UI_Major").GetComponent<UI_Major>();
        mUIWindowDic.Add(UIWindow.Major,major);
        UI_SingleFB simpleFB = mRoot.transform.Find("Canvas/UI_SingleFB").GetComponent<UI_SingleFB>();
        mUIWindowDic.Add(UIWindow.SingleFuben,simpleFB);

        OpenWindow(UIWindow.Tips,true);
        
        this.Log("UISvc Init...");
    }

    public void Tick()
    {
        
    }

    public void UnInit()
    {
        this.Log("UISvc UnInit...");
    }

    /// <summary>
    /// 设置加载进度
    /// </summary>
    /// <param name="process">设置的进度</param>
    /// <param name="state">是否打开UI加载界面</param>
    public void SetLoading(int process,bool state = true)
    {
        mUILoading.SetProcess(process,state);
    }

    /// <summary>
    /// 切换关卡的基础层UI，当切换新模式的时候，将上个模式的UI全部关闭
    /// </summary>
    /// <param name="mode"></param>
    public void SwitchStageBaseUI(CPlayMode mode)
    {
        UIWindow window = UIWindow.None;
        switch (mode)
        {
            case CPlayMode.Login:
                window = UIWindow.Login;
                break;
            case CPlayMode.Create:
                window = UIWindow.Create;
                break;
            case CPlayMode.Major:
                window = UIWindow.Major;
                break;
            case CPlayMode.Wild:
                window = UIWindow.Wild;
                break;
            case CPlayMode.Single:
                break;
            case CPlayMode.Multiple:
                break;
            case CPlayMode.Activity:
                break;
            case CPlayMode.Guild:
                break;
            case CPlayMode.Marriage:
                break;
            case CPlayMode.Fishing:
                break;
            case CPlayMode.Concert:
                break;
        }

        //为什么这里要加windowValue.WindowLayer == WindowLayer.PopUI
        //是因为这里方法进入的都是BaseUI层的UI，但是mUIWindowDic是有PopUI的，当我们切换模式的时候，需要将上个模式中的所有Pop层的UI关闭
        //当PopUI进入后，UIWindow的Type不会等于上述设置的type，因此会调用SetWindowState(false)进行关闭
        foreach (var windowType in mUIWindowDic.Keys)
        {
            WindowRoot windowValue = mUIWindowDic[windowType];
            if (windowValue.WindowLayer == WindowLayer.BaseUI || windowValue.WindowLayer == WindowLayer.PopUI)
            {
                if (windowType == window)
                {
                    windowValue.SetWindowState(true);
                }
                else
                {
                    windowValue.SetWindowState(false);
                }


            }

        }
        
    }

    /// <summary>
    /// 打开任意窗口
    /// </summary>
    /// <param name="window"></param>
    public void OpenWindow(UIWindow window,bool state)
    {
        if (mUIWindowDic.ContainsKey(window))
        {
            mUIWindowDic[window].SetWindowState(state);
        }
        else
        {
            this.LogRed($"该窗口{window}不存在!");
        }
    }

    /// <summary>
    /// 显示弹窗
    /// </summary>
    /// <param name="msg"></param>
    public void ShowTips(string msg)
    {
        mTips.AddTips(msg);
    }

    #region Tools

    public GameObject CloneGo(GameObject prefab)
    {
        GameObject go = null;
        if (prefab != null)
        {
            go = Object.Instantiate(prefab);
            if (go.name.Contains("(Clone)"))
            {
               go.name = go.name.Replace("(Clone)", "");
            }
        }
        return go;
    }

    /// <summary>
    /// 子节点遍历委托
    /// </summary>
    /// <param name="index">索引</param>
    /// <param name="child">节点</param>
    public delegate void EachChildHandler(int index, Transform child);

    /// <summary>
    /// 添加子物体
    /// </summary>
    /// <param name="parent">父节点</param>
    /// <param name="prefab">节点原型</param>
    /// <param name="layer">节点层级</param>
    /// <returns></returns>
    public static GameObject AddChild(Transform parent, GameObject prefab, int layer = -1)
    {
        GameObject go = GameObject.Instantiate(prefab);
        if (go != null && parent != null)
        {
            Transform t = go.transform;
            t.SetParent(parent.transform);
            t.localPosition = Vector3.zero;
            t.localRotation = Quaternion.identity;
            t.localScale = Vector3.one;
            if (layer == -1) go.layer = parent.gameObject.layer;
            else if (layer > -1 && layer < 32) go.layer = layer;
        }

        return go;
    }

    /// <summary>
    /// 确保子物体数量（若不足则拷贝补足）
    /// </summary>
    /// <param name="parent">父节点</param>
    /// <param name="count">子节点个数</param>
    /// <param name="active">状态</param>
    /// <param name="prefab">节点原型</param>
    public static void EnsureChild(Transform parent, int count, bool active = false, GameObject prefab = null)
    {
        if (parent.childCount < count)
        {
            count -= parent.childCount;
            if (prefab == null) prefab = parent.GetChild(0).gameObject;
            for (int i = 0; i < count; i++)
            {
                var child = AddChild(parent, prefab);
                child.SetActive(active);
            }
        }
    }

    /// <summary>
    /// 遍历所有子物体
    /// </summary>
    /// <param name="parent">父节点</param>
    /// <param name="handler">回调函数</param>
    public static void EachChild(Transform parent, EachChildHandler handler)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            var child = parent.GetChild(i);
            handler?.Invoke(i, child);
        }
    }

    /// <summary>
    /// 显示所有子物体
    /// </summary>
    /// <param name="parent">父节点</param>
    /// <param name="handler">回调函数</param>
    public static void ShowChild(Transform parent, EachChildHandler handler = null)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            var child = parent.GetChild(i);
            child.gameObject.SetActive(true);
            handler?.Invoke(i, child);
        }
    }

    #endregion
}
